﻿define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Proorder, GoPage, GoDialog, $ionicPopup) {

		$scope.init_data = function() {
			/*
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			var all = window.innerHeight;
			$scope.myObj = {
				"height": (all - lan - 100) + "px"
			}
			*/
			Proorder.getList(1, function(data) {
				$scope.promotionList = data.result.promotionList;
				//console.log($scope.promotionList)
			})
		}
		$scope.init_data();

		//跳转后刷新页面  /*******慎用********/
		$scope.$on('$ionicView.beforeEnter', function() {
			///局部刷新，更新所需的字段
			$scope.init_data();
		});
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			GoPage.init_top($scope, 44,44);

		}, false);
		//跳转页面
		$scope.show_page = function(id, act_id, index) {
			if(id == 1) {
				GoPage.go_states("addpro", {
					'act_id': act_id
				});
			}
			if(id == 2) {
				GoDialog.confirm("提示", "确认要删除该促销么？", function() {
					Proorder.pro_delete(act_id, function(data) {
						if(data.success) { //数据库里删除成功，才能在前台删除
							GoDialog.toast("删除成功！");
							$scope.delete_brandItem($scope.promotionList, act_id);
						} else {
							GoDialog.toast(data.msg);
						}
					})
				})
			}
			if(id == 3) {
				GoPage.go_states("prosaleorder", {
					'act_id': act_id
				});
			}
		}

		$scope.delete_brandItem = function(datas, act_id) {
			var index = -1;
			for(var i = 0; i < datas.length; i++) {
				if(datas[i].act_id == act_id) {
					index = i;
					break; //一找到就退出循环
				}
			}
			if(index != -1) { //说明找到对应的商品
				datas.splice(index, 1);
			}
		};

		//添加促销
		$scope.onshow_addpro = function() {
			GoPage.go_states("addpro", {});
		}

		//下拉刷新
		$scope.input = new Object();
		$scope.input.nPage = 1;
		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Proorder.getList(1, function(data) {
					if(data.result.promotionList != null) {
						$scope.promotionList = data.result.promotionList;;
					} else {
						$scope.promotionList = new Object();
					}
					$scope.input.nPage = 1;
				},
				function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});

		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Proorder.getList($scope.input.nPage + 1, function(data) {
					if(data.result.promotionList != null) {
						if($scope.promotionList != undefined) {
							$scope.promotionList = $scope.promotionList.concat(data.result.promotionList);
							$scope.input.nPage += 1;
						} else {
							$scope.promotionList = data.result.promotionList;
						}
					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});

		};

	}

	controller.$inject = ['$scope', 'Proorder', 'GoPage', 'GoDialog', '$ionicPopup'];

	return app.registerController("proorderctrl", controller);
});